package com.uphone.kingmall.base;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/18
 *     desc   :
 * </pre>
 */
public abstract class BaseListActivity<B extends BaseBean, T extends BaseBean.DataBean> extends BaseActivity {

    @BindView(R.id.recyclerview)
    protected RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll)
    protected LinearLayout ll;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    protected int page = -1;
    protected BaseQuickAdapter mAdapter;
    protected List<T> dataList = new ArrayList<>();
//    private boolean isLoad;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_base_list;
    }

    @Override
    public void initView() {
        DefaultNavigationBar navigationBar = CommonUtil.initTitle(this, initTitle(), ll);
        setTitle(navigationBar);
        GridLayoutManager manager = getGridLayoutManager();
        if (manager != null) {
            recyclerview.setLayoutManager(manager);
        } else {
            recyclerview.setLayoutManager(new LinearLayoutManager(this));
        }
        mAdapter = initAdapter();
        recyclerview.setAdapter(mAdapter);

        initListener();
    }

    protected void setTitle(DefaultNavigationBar navigationBar) {

    }

    protected GridLayoutManager getGridLayoutManager() {
        return null;
    }

    protected abstract String initTitle();

    private void initListener() {

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                itemClick(adapter, view, position);
            }
        });


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                loadData(1);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                loadData(++page);
            }
        });
    }

    protected void loadData(int page) {
//        if (isLoad) {
//            return;
//        }
//        isLoad = true;
        this.page = page;
        CommonUtil.netRv(getBeans(), mAdapter, dataList, getUrl(), getParams(), page, new NetListCallBack<T>() {
            @Override
            public void callBack(List<T> list) {
//                isLoad = false;
                dataList = list;
                dataCallBack(list);

                if (dataList == null || dataList.size() == 0) {
                    tvEmpty.setVisibility(View.VISIBLE);
                } else {
                    tvEmpty.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 条目点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    protected abstract void itemClick(BaseQuickAdapter adapter, View view, int position);

    /**
     * 初始化适配器
     *
     * @return
     */
    protected abstract BaseQuickAdapter initAdapter();

    @Override

    public void initData() {
        loadData(1);
    }

    protected abstract B getBeans();

    protected void dataCallBack(List<T> list) {
    }

    protected abstract HttpParams getParams();

    protected abstract String getUrl();
}
